package selcoursesys.dao.impl;

import db.MyHibernateSessionFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import selcoursesys.dao.CourseDAO;
import selcoursesys.entity.*;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Lpbdz on 2017/4/9.
 */
public class CourseDAOImpo extends BaseHibernateDAOImpl implements CourseDAO {
    private Session session;
    public boolean delCourse(CourseEntity courseEntity){
        Transaction tx=null;
        try{
            session=MyHibernateSessionFactory.getSession();
            tx=session.beginTransaction();
            String hql="delete CourseEntity course where course.courseid=:courseid";
            Query query=session.createQuery(hql);
            query.setString("courseid",courseEntity.getCourseid());
            query.executeUpdate();
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
            return false;
        }finally {
            MyHibernateSessionFactory.closeSession();
            return true;
        }
    }
    //public boolean updateCourse(CourseEntity courseEntity){}
    public boolean checkCourse(CourseEntity courseEntity){
        session=MyHibernateSessionFactory.getSession();
        session.clear();
        String hql="From CourseEntity";
        Query query=session.createQuery(hql);
        //query.setString("upwd",user.getUpwd());
        List list=query.list();
        MyHibernateSessionFactory.closeSession();
        if(list.size()>0){
            return true;
        }else{
            return false;
        }
    }

    public boolean insertCousrse(CourseEntity courseEntity){
        Transaction tx=null;
        try{
            session=MyHibernateSessionFactory.getSession();
            tx=session.beginTransaction();
            session.save(courseEntity);
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
            return false;
        }finally {
            MyHibernateSessionFactory.closeSession();
            return true;
        }
    }
    public List<CourseEntity> queryCourse(CourseEntity courseEntity){
        session=MyHibernateSessionFactory.getSession();
        session.clear();
        /**
         * hql 条件查询
         * String hql="From UserEntity usee where usee.utype=:tea";
         * Query query=session.createQuery(hql);
         * query.setString("tea","tea");
         *
         * */
        String hql="From CourseEntity course where course.courseid=:courseid";
        Query query=session.createQuery(hql);
        query.setString("courseid",courseEntity.getCourseid());
        List list=null;
        list=query.list();
        return list;
    }

    public CourseEntity  queryCourseId(CourseEntity courseEntity){
        CourseEntity courseEntity1=null;
        session=MyHibernateSessionFactory.getSession();
        session.clear();
        String hql="From CourseEntity course where course.courseid=:courseid";
        Query query=session.createQuery(hql);
        query.setString("courseid",courseEntity.getCourseid());
        List list=query.list();
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            courseEntity1=(CourseEntity)iterator.next();
            return courseEntity1;
        }
        MyHibernateSessionFactory.closeSession();
        //CourseEntity courseEntity1=(CourseEntity)super.get(CourseEntity.class,courseEntity.getCourseid());
        return courseEntity1;
    }

    public List<CourseEntity> queryCourseAid(String cid){
        session=MyHibernateSessionFactory.getSession();
        session.clear();
        /**
         * hql 条件查询
         * String hql="From UserEntity usee where usee.utype=:tea";
         * Query query=session.createQuery(hql);
         * query.setString("tea","tea");
         *
         * */
        String hql="From CourseEntity course where course.courseid like ? and course.coursetype=:coursetype";
        Query query=session.createQuery(hql);

        query.setString(0,"%"+cid+"%");

        query.setString("coursetype","选修");

        List list=query.list();
        //System.out.println("shujuku"+cid);
        //System.out.println("end");
        MyHibernateSessionFactory.closeSession();
        return list;
    }
    public boolean updateCourse(CourseEntity courseEntity){
        Transaction tx=null;
        try{
            session=MyHibernateSessionFactory.getSession();
            tx=session.beginTransaction();
            String hql="update CourseEntity usee set lnum=:lnum where usee.courseid=:courseid";
            Query query=session.createQuery(hql);
            query.setString("lnum",courseEntity.getLnum().toString());
            query.setString("courseid",courseEntity.getCourseid());
            query.executeUpdate();
            tx.commit();
        }catch(Exception e){
            session.close();
            e.printStackTrace();
            if(tx!=null){
                tx.rollback();
            }
            return false;
        }finally{
            MyHibernateSessionFactory.closeSession();
            return true;
        }
    }

    //根据tid得到教师所授课程详细
    public CourseEntity queryCourseTid(String tid) {
        session = MyHibernateSessionFactory.getSession();
        session.clear();
        String hql = "From CourseEntity course where course.teacherEntity.tid=:tid";
        Query query = session.createQuery(hql);
        query.setString("tid", tid);
        List list = null;
        list = query.list();
        MyHibernateSessionFactory.closeSession();
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            CourseEntity courseEntity = (CourseEntity) iterator.next();
            return courseEntity;
        }
        return null;
    }
    //查看满足条件的课程
    public List<CourseEntity> queryCourseAll(String ctype,String term,String majorAb){
        session = MyHibernateSessionFactory.getSession();
        session.clear();
        Query query;
        if(ctype.equals("ALL")){
            String hql = "From CourseEntity course where course.courseid like ?";
            query = session.createQuery(hql);
            query.setString(0,("%"+majorAb+term+"%"));
        }else{
            String hql = "From CourseEntity course where course.courseid like ? and course.coursetype like ?";
            query = session.createQuery(hql);
            query.setString(1,("%"+ctype+"%"));
            query.setString(0,("%"+majorAb+term+"%"));
        }
        List list = null;
        list = query.list();
        MyHibernateSessionFactory.closeSession();
        return list;
    }

    //根据课程id查询课程
    public CourseEntity queryCourseById(String courseid){
        session = MyHibernateSessionFactory.getSession();
        session.clear();
        String hql = "From CourseEntity course where course.courseid=:courseid";
        Query query=session.createQuery(hql);
        query.setString("courseid",courseid);
        List list=query.list();
        MyHibernateSessionFactory.closeSession();
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            CourseEntity courseEntity=(CourseEntity)iterator.next();
            return courseEntity;
        }
        return null;
    }

    public List<CourseEntity> queryAllCourse(){
        session = MyHibernateSessionFactory.getSession();
        session.clear();
        String hql = "From CourseEntity";
        Query query=session.createQuery(hql);
        List list=query.list();
        MyHibernateSessionFactory.closeSession();
        if(list.size()>0&&list!=null){
            return list;
        }
        return null;
    }

    public List<CourseEntity> queryCourseCourseName(String keyword){
        session = MyHibernateSessionFactory.getSession();
        session.clear();
        String hql = "From CourseEntity course where course.coursename like ?";
        Query query=session.createQuery(hql);
        query.setString(0,"%"+keyword+"%");
        List list=query.list();
        MyHibernateSessionFactory.closeSession();
        if(list.size()>0&&list!=null){
            return list;
        }
        return null;
    }
}
